package dao;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jdbc.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TeacherDaoImp implements TeacherDao{
    /**
     * 通过登录用户名找到id
     * @param userName
     * @return 返回id
     */
    @Override
    public int teacherID(String userName) throws SQLException {
        jdbc jdbc=new jdbc();
        Connection connection=jdbc.lianjie();
        String sql="SELECT id FROM schooltreasure.teacher where userName='"+userName+"';";

        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        ResultSet resultSet=preparedStatement.executeQuery();

       int id=0;
        while (resultSet.next()){
           id=resultSet.getInt("id");
        }

        jdbc.guanbi(null,preparedStatement,connection);
        return id;

    }

    /**
     * 下拉列表查老师名字
     * @return
     */
    @Override
    public JSONArray showTea() throws SQLException {
        jdbc jdbc=new jdbc();
        Connection connection=jdbc.lianjie();
        String sql="SELECT * FROM schooltreasure.teacher;";

        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        ResultSet res=preparedStatement.executeQuery();
        JSONArray jsonArray=new JSONArray();
        while (res.next()){
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("id",res.getInt("id"));
            jsonObject.put("teacherName",res.getString("teacherName"));
            jsonObject.put("userName",res.getString("userName"));
            jsonObject.put("password",res.getInt("password"));
            jsonObject.put("level",res.getInt("level"));
            jsonObject.put("invalid",res.getInt("invalid"));
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }
}
